﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Windows.Forms;

namespace EnviamentDeDocuments
{
    class PropertiesFileParser
    {

        private string _pathToFile = null;
        Dictionary<string, string> _properties;

        public PropertiesFileParser(String path)
        {
            this._pathToFile = path;            
        }


        public String getPathToFile()
        {
            return _pathToFile;
        }

        public Dictionary<string, string> getProperties()
        { 
            if (_properties == null)
                parseData();

            return _properties;
        }

        private void parseData()
        {
            _properties = new Dictionary<string, string>();
            
            foreach (String row in File.ReadAllLines(_pathToFile,Encoding.Default))
            {
                if (!row.Trim().StartsWith("#") && row.Contains("="))
                {
                    String key = row.Split('=')[0];
                    String value = Decoder(string.Join("=", row.Split('=').Skip(1).ToArray()));

                    _properties.Add(key, value);
                }
            }            
        }

        private static Regex _regex = new Regex(@"\\u(?<Value>[a-zA-Z0-9]{4})", RegexOptions.Compiled);
        public string Decoder(string value)
        {
            return _regex.Replace(value, converter);
        }

        
        private string converter(Match match)
        {
            return ((char)int.Parse(match.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
        }

    
    }
}
